package com.xiaotu.utils;

import com.artofsolving.jodconverter.DocumentConverter;
import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;
import com.artofsolving.jodconverter.openoffice.converter.StreamOpenOfficeDocumentConverter;

import java.io.File;
import java.io.IOException;
import java.net.ConnectException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

/**
 * @author 张晓
 * @create 2021-07-29 23:15
 */
public class JodConverterUtil {

    /**
     * 转换文件成pdf
     *
     * @throws IOException
     */
    public static void officeToPDF(String sourceFile, String destFile) {
        try {
            long old = System.currentTimeMillis();
            // OpenOffice的安装目录
//            String OpenOffice_HOME = "D:/OpenOffice4";
//            if (OpenOffice_HOME.charAt(OpenOffice_HOME.length() - 1) != '/') {
//                OpenOffice_HOME += "/";
//            }
//            Process process = null;
//            // 启动OpenOffice的服务
//            String command = OpenOffice_HOME
//                    + "program/soffice.exe -headless -accept=\"socket,host=127.0.0.1,port=8100;urp;\" -nofirststartwizard";
//            process = Runtime.getRuntime().exec(command);

            Properties property = PropertiesUitls.fetchProperties("/config.properties");

            File inputFile = new File(sourceFile);
            if (!inputFile.exists()) {
                // 找不到源文件, 则返回false
                throw new IllegalArgumentException("找不到源文件");
            }
            // 如果目标路径不存在, 则新建该路径
            File outputFile = new File(destFile);
            if (!outputFile.getParentFile().exists()) {
                outputFile.getParentFile().mkdirs();
            }
            //如果目标文件存在，则删除
            if (outputFile.exists()) {
                outputFile.delete();
            }
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            OpenOfficeConnection connection = new SocketOpenOfficeConnection(property.getProperty("OFFICE_IP_NUMBER"), Integer.parseInt(property.getProperty("OFFICE_PORT_NUMBER")));
            connection.connect();
            //openOffice连接时间
            System.out.println("连接时间:" + df.format(new Date()));
            DocumentConverter converter = new StreamOpenOfficeDocumentConverter(
                    connection);

            converter.convert(inputFile, outputFile);
            long now = System.currentTimeMillis();
            //word转PDF的转换时间
            connection.disconnect();
            // 销毁OpenOffice服务的进程
            //process.destroy();
            System.out.println("word2Pdf共耗时：" + ((now - old) / 1000.0) + "秒");  //转化用时
        } catch (ConnectException e) {
            e.printStackTrace();
            System.err.println("openOffice连接失败！请检查IP,端口");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        String docPath = "E:\\360MoveData\\Users\\Administrator\\Desktop\\2-文化产业园区申报申请表.docx";
        String pdfPath = "E:\\360MoveData\\Users\\Administrator\\Desktop\\3-文化产业园区申报申请表.pdf";
        officeToPDF(docPath, pdfPath);
    }

}
